<template>
  <div class="app-container">
    <el-form
      :model="formData"
      ref="formData"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="项目名称">
        <el-select
          v-model="formData.projectId"
          filterable
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in productList"
            :key="item.id"
            :label="item.projectName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="日期范围">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          :picker-options="pickerOptions"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          plain
          :loading="btnLoading"
          size="mini"
          :disabled="!formData.projectId"
          @click="onReCount"
          >重新统计</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 表格   -->
    <el-table :loading="loading" :data="tableData">
      <el-table-column label="日期" align="center" prop="projcetTime" />
      <el-table-column label="呼叫总数" align="center" prop="callTotal" />
      <el-table-column label="呼入数" align="center" prop="callInNumber" />
      <el-table-column label="应答数" align="center" prop="answerNumber" />
      <el-table-column label="应答率" align="center" prop="answerRate" />
      <el-table-column label="转接数" align="center" prop="switchNumber" />
      <el-table-column
        label="转接率"
        align="center"
        prop="switchRate"
      ></el-table-column>
      <el-table-column label="呼损数" align="center" prop="callBadNumber" />
      <el-table-column label="呼损率" align="center" prop="callBadRate" />
      <el-table-column label="成功数" align="center" prop="successNumber" />
      <el-table-column label="成功率" align="center" prop="successRate" />
      <el-table-column label="BOT消费（元）" align="center" prop="botBill" />
      <el-table-column label="话费金额（元）" align="center" prop="phoneBill" />
      <el-table-column label="分钟数" align="center" prop="minuteNumber" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" @click="onPeriod(scope.row)" type="text"
            >时段统计</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="pagination.pageNum"
      :limit.sync="pagination.pageSize"
      @pagination="getList"
    />

    <projectDetailDialog
      :periodDialogShow="periodDialogShow"
      :id="currentId"
      @onClosePeriodDialog="onClosePeriodDialog"
    ></projectDetailDialog>
  </div>
</template>

<script>
import { projectFormsList, getChangeForms } from "@/api/dataReport/project.js";
import projectDetailDialog from "./components/projectDetailDialog.vue";
import { projectList } from "@/api/projectManagement/list";
import { Message } from "element-ui";
export default {
  name: "projectReport",
  components: { projectDetailDialog },
  data() {
    return {
      loading: false,
      tableData: [], //表格数据
      showSearch: true, //显示搜索条件
      // 查询参数
      formData: {
        projectId: "", //名称
      },
      statusList: [
        { value: 1, label: "禁用" },
        { value: 0, label: "启用" },
      ], //项目状态
      pagination: {
        pageNum: 1,
        pageSize: 10,
      },
      btnLoading: false,
      idList: [],
      total: 0,
      currentId: "",
      multiple: true,
      dateRange: [], //日期范围
      periodDialogShow: false,
      productList: [],
      pickerOptions: {
        onPick: (time) => {
          // 选择开始时间未选择结束时间
          if (time.minDate && !time.maxDate) {
            this.timeOptionRange = time.minDate;
          }
          if (time.maxDate) {
            this.timeOptionRange = null;
          }
        },
        disabledDate: (time) => {
          let timeOptionRange = this.timeOptionRange;

          let secondNum = 1000 * 60 * 60 * 24 * 6;
          let times = new Date(
            new Date(new Date().toLocaleDateString()).getTime() +
              24 * 60 * 60 * 1000 -
              1
          );
          if (!timeOptionRange) {
            return time.getTime() > times;
          }
          if (timeOptionRange.getTime() + secondNum > times) {
            return (
              time.getTime() > times ||
              time.getTime() < timeOptionRange.getTime() - secondNum
            );
          }
          return (
            time.getTime() < timeOptionRange.getTime() ||
            time.getTime() > timeOptionRange.getTime() + secondNum
          );
        },
      },
    };
  },
  async created() {
    this.getProductList();
    this.getList();
  },
  methods: {
    // 列表查询
    async getList() {
      const params = {
        ...this.formData,
        ...this.pagination,
      };

      this.loading = true;
      const { total, rows } = await projectFormsList(
        this.addDateRange(params, this.dateRange)
      );
      this.total = total;
      this.tableData = rows;
      this.loading = false;
    },

    onPeriod(row) {
      this.currentId = String(row?.id) || "";
      this.periodDialogShow = true;
    },

    // 项目列表
    async getProductList() {
      const params = {
        status: "",
        projectId: "",
        pageNum: 1,
        pageSize: 1000,
      };
      const { code, rows } = await projectList(params);
      if (code === 200) {
        this.productList = rows.filter((el) => {
          return el.status === "启用";
        });
      }
    },

    onClosePeriodDialog() {
      this.currentId = "";
      this.periodDialogShow = false;
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.pagination.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.formData = {
        projectId: "",
      };
      this.dateRange = [];
      this.handleQuery();
    },

    // 重新统计
    async onReCount() {
      this.btnLoading = true;
      const params = {
        projectId: this.formData.projectId,
      };
      try {
        const { code, msg } = await getChangeForms(params);
        if (code === 200) {
          this.$message.success(msg);
          this.getList();
        }
      } catch (error) {
      } finally {
        this.btnLoading = false;
      }
    },

    /** 导出按钮操作 */
    handleExport() {
      const url = `/system/projectForms/export`;
      const formData = JSON.parse(JSON.stringify(this.formData));
      let data = {
        ...formData,
        ...this.pagination,
      };
      if (this.dateRange?.length) {
        data.params =
          typeof data.params === "object" &&
          data.params !== null &&
          !Array.isArray(data.params)
            ? data.params
            : {};
        data.params["beginTime"] = this.dateRange[0];
        data.params["endTime"] = this.dateRange[1];
      }
      this.download(
        url,
        {
          ...data,
        },
        `projectReport_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style lang="scss" scoped></style>
